package com.example.my_flea_street.controller;

import com.example.my_flea_street.bean.Info;
import com.example.my_flea_street.bean.InfoPage;
import com.example.my_flea_street.bean.ResponseModel;
import com.example.my_flea_street.service.impl.SquareServiceImpl;
import com.example.my_flea_street.util.ResponseHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(value = "/square")
public class SquareController {

    private final SquareServiceImpl squareService;

    @Autowired
    public SquareController(SquareServiceImpl squareService) {
        this.squareService = squareService;
    }

    @RequestMapping(value = "/getInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<ResponseModel> getInfo(@RequestBody Map<String, String> json ) {
       int pageNum = Integer.parseInt(json.get("page_no"));
        int pageSize = Integer.parseInt(json.get("page_size"));
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Info> pageInfo = new PageInfo<>(squareService.getInfo());
        InfoPage page = new InfoPage();
        page.setTotal((int)pageInfo.getTotal());
        page.setPages(pageInfo.getPages());
        page.setPageNum(pageInfo.getPageNum());
        page.setData(pageInfo.getList());
        return ResponseHelper.createResponse(0, "成功",page);
    }
    @RequestMapping(value = "/reportInfo",method = {RequestMethod.POST,RequestMethod.GET})
    public ResponseEntity<ResponseModel> reportInfo(@RequestBody Map<String, String> json) {
        String release_time = json.get("release_time");
        int infoId = Integer.parseInt(json.get("info_id"));
        squareService.report(infoId,release_time);
        return ResponseHelper.createResponse(0, "成功");
    }
}
